
/*
 * Copyright 2002-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
 *版权所有2002-2023原作者。
 *
 *根据Apache许可证2.0版许可（“许可证”）；
 *除非符合许可证的规定，否则您不得使用此文件。
 *您可以在以下网址获取许可证副本：
 *
 *https://www.apache.org/licenses/LICENSE-2.0
 *
 *除非适用法律要求或书面同意，软件
 *根据许可证进行的分发是在“按原样”的基础上进行的，
 *无任何明示或暗示的保证或条件。
 *有关管理权限的特定语言，请参阅许可证和
 *许可证下的限制。
 */

package org.springframework.ui.context.support;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.context.HierarchicalMessageSource;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.lang.Nullable;
import org.springframework.ui.context.HierarchicalThemeSource;
import org.springframework.ui.context.Theme;
import org.springframework.ui.context.ThemeSource;

/**
 * {@link ThemeSource} implementation that looks up an individual
 * {@link java.util.ResourceBundle} per theme. The theme name gets
 * interpreted as ResourceBundle basename, supporting a common
 * basename prefix for all themes.
 *
 * @author Jean-Pierre Pawlak
 * @author Juergen Hoeller
 * @see #setBasenamePrefix
 * @see java.util.ResourceBundle
 * @see org.springframework.context.support.ResourceBundleMessageSource
 * @deprecated as of 6.0 in favor of using CSS, without direct replacement
 */
/**
 *｛@link-ThemeSource｝实现，该实现查找单个
 *｛@link java.util.ResourceBundle｝每个主题。主题名称得到
 *解释为ResourceBundle basename，支持
 *所有主题的basename前缀。
 *
 *@作者Jean-Pierre Pawlak
 *@作者于尔根·霍勒
 *@参见#setBasenamePrefix
 *@参见java.util.ResourceBundle
 *@参见org.springframework.context.support.ResourceBundleMessageSource
 *@从6.0起已弃用，支持使用CSS，无需直接替换
 */
@Deprecated(since = "6.0")
public class ResourceBundleThemeSource implements HierarchicalThemeSource, BeanClassLoaderAware {

	protected final Log logger = LogFactory.getLog(getClass());

	@Nullable
	private ThemeSource parentThemeSource;

	private String basenamePrefix = "";

	@Nullable
	private String defaultEncoding;

	@Nullable
	private Boolean fallbackToSystemLocale;

	@Nullable
	private ClassLoader beanClassLoader;

	/** Map from theme name to Theme instance. */
	private final Map<String, Theme> themeCache = new ConcurrentHashMap<>();


	@Override
	public void setParentThemeSource(@Nullable ThemeSource parent) {
		this.parentThemeSource = parent;

		// Update existing Theme objects.
		// 更新现有主题对象。
		// Usually there shouldn't be any at the time of this call.
		// 通常在打电话的时候不应该有。
		synchronized (this.themeCache) {
			for (Theme theme : this.themeCache.values()) {
				initParent(theme);
			}
		}
	}

	@Override
	@Nullable
	public ThemeSource getParentThemeSource() {
		return this.parentThemeSource;
	}

	/**
	 * Set the prefix that gets applied to the ResourceBundle basenames,
	 * i.e. the theme names.
	 * E.g.: basenamePrefix="test.", themeName="theme" &rarr; basename="test.theme".
	 * <p>Note that ResourceBundle names are effectively classpath locations: As a
	 * consequence, the JDK's standard ResourceBundle treats dots as package separators.
	 * This means that "test.theme" is effectively equivalent to "test/theme",
	 * just like it is for programmatic {@code java.util.ResourceBundle} usage.
	 * @see java.util.ResourceBundle#getBundle(String)
	 */
	/** Map from theme name to Theme instance. */
	private final Map＜String，Theme＞themeCache＝new ConcurrentHashMap＜＞（）；


	@覆盖
	public void setParentThemeSource（@Nullable ThemeSource父级）{
		this.parentThemeSource=父级；

		已同步（this.themeCache）{
			for（主题主题：this.themeCache.values（））{
				initParent（主题）；
			}
		}
	}

	@覆盖
	@可为null
	公共主题源getParentThemeSource（）{
		返回this.parentThemeSource；
	}

	/**
	 *设置应用于ResourceBundle基名称的前缀，
	 *即主题名称。
	 *例如：basenamePrefix=“test.”，themeName=“theme”&rarr；basename=“test.theme”。
	 *<p>请注意，ResourceBundle名称实际上是类路径位置：作为
	 *因此，JDK的标准ResourceBundle将点视为包分隔符。
	 *这意味着“test.theme”实际上等同于“test/theme”，
	 *就像它用于编程{@code java.util.ResourceBundle}使用一样。
	 *@参见java.util.ResourceBundle#getBundle（字符串）
	 */
	public void setBasenamePrefix(@Nullable String basenamePrefix) {
		this.basenamePrefix = (basenamePrefix != null ? basenamePrefix : "");
	}

	/**
	 * Set the default charset to use for parsing resource bundle files.
	 * <p>{@link ResourceBundleMessageSource}'s default is the
	 * {@code java.util.ResourceBundle} default encoding: ISO-8859-1.
	 * @since 4.2
	 * @see ResourceBundleMessageSource#setDefaultEncoding
	 */
	/**
	 *设置用于解析资源绑定文件的默认字符集。
	 *＜p＞｛@link ResourceBundleMessageSource｝的默认值是
	 *｛@code java.util.ResourceBundle｝默认编码：ISO-8859-1。
	 *@自4.2起
	 *@请参阅ResourceBundleMessageSource#setDefaultEncoding
	 */
	public void setDefaultEncoding(@Nullable String defaultEncoding) {
		this.defaultEncoding = defaultEncoding;
	}

	/**
	 * Set whether to fall back to the system Locale if no files for a
	 * specific Locale have been found.
	 * <p>{@link ResourceBundleMessageSource}'s default is "true".
	 * @since 4.2
	 * @see ResourceBundleMessageSource#setFallbackToSystemLocale
	 */
	/**
	 *设置如果没有文件，是否回退到系统区域设置
	 *已找到特定的区域设置。
	 *＜p＞｛@link ResourceBundleMessageSource｝的默认值为“true”。
	 *@自4.2起
	 *@请参阅ResourceBundleMessageSource#setFallbackToSystemLocale
	 */
	public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
		this.fallbackToSystemLocale = fallbackToSystemLocale;
	}

	@Override
	public void setBeanClassLoader(@Nullable ClassLoader beanClassLoader) {
		this.beanClassLoader = beanClassLoader;
	}


	/**
	 * This implementation returns a SimpleTheme instance, holding a
	 * ResourceBundle-based MessageSource whose basename corresponds to
	 * the given theme name (prefixed by the configured "basenamePrefix").
	 * <p>SimpleTheme instances are cached per theme name. Use a reloadable
	 * MessageSource if themes should reflect changes to the underlying files.
	 * @see #setBasenamePrefix
	 * @see #createMessageSource
	 */
	/**
	 *此实现返回一个SimpleTheme实例，其中包含
	 *基于ResourceBundle的MessageSource，其基名称对应于
	 *给定的主题名称（前缀为配置的“basenamePrefix”）。
	 *<p>SimpleTheme实例按主题名称缓存。使用可重新加载的
	 *MessageSource如果主题应反映对基础文件的更改。
	 *@参见#setBasenamePrefix
	 *@参见#createMessageSource
	 */
	@Override
	@Nullable
	public Theme getTheme(String themeName) {
		Theme theme = this.themeCache.get(themeName);
		if (theme == null) {
			synchronized (this.themeCache) {
				theme = this.themeCache.get(themeName);
				if (theme == null) {
					String basename = this.basenamePrefix + themeName;
					MessageSource messageSource = createMessageSource(basename);
					theme = new SimpleTheme(themeName, messageSource);
					initParent(theme);
					this.themeCache.put(themeName, theme);
					if (logger.isDebugEnabled()) {
						logger.debug("Theme created: name '" + themeName + "', basename [" + basename + "]");
					}
				}
			}
		}
		return theme;
	}

	/**
	 * Create a MessageSource for the given basename,
	 * to be used as MessageSource for the corresponding theme.
	 * <p>Default implementation creates a ResourceBundleMessageSource.
	 * for the given basename. A subclass could create a specifically
	 * configured ReloadableResourceBundleMessageSource, for example.
	 * @param basename the basename to create a MessageSource for
	 * @return the MessageSource
	 * @see org.springframework.context.support.ResourceBundleMessageSource
	 * @see org.springframework.context.support.ReloadableResourceBundleMessageSource
	 */
	/**
	 *为给定的基名称创建MessageSource，
	 *用作相应主题的MessageSource。
	 *＜p＞默认实现创建一个ResourceBundleMessageSource。
	 *对于给定的基名称。子类可以创建
	 *配置了ReloadableResourceBundleMessageSource。
	 *@param basename要为其创建MessageSource的basename
	 *@return消息源
	 *@参见org.springframework.context.support.ResourceBundleMessageSource
	 *@请参阅org.springframework.context.support.ReloadableResourceBundleMessageSource
	 */
	protected MessageSource createMessageSource(String basename) {
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.setBasename(basename);
		if (this.defaultEncoding != null) {
			messageSource.setDefaultEncoding(this.defaultEncoding);
		}
		if (this.fallbackToSystemLocale != null) {
			messageSource.setFallbackToSystemLocale(this.fallbackToSystemLocale);
		}
		if (this.beanClassLoader != null) {
			messageSource.setBeanClassLoader(this.beanClassLoader);
		}
		return messageSource;
	}

	/**
	 * Initialize the MessageSource of the given theme with the
	 * one from the corresponding parent of this ThemeSource.
	 * @param theme the Theme to (re-)initialize
	 */
	/**
	 *使用初始化给定主题的MessageSource
	 *一个来自该主题源的对应父级。
	 *@param theme要（重新）初始化的主题
	 */
	protected void initParent(Theme theme) {
		if (theme.getMessageSource() instanceof HierarchicalMessageSource messageSource) {
			if (getParentThemeSource() != null && messageSource.getParentMessageSource() == null) {
				Theme parentTheme = getParentThemeSource().getTheme(theme.getName());
				if (parentTheme != null) {
					messageSource.setParentMessageSource(parentTheme.getMessageSource());
				}
			}
		}
	}

}
